layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    /**
     * VOCs综合减排措施管理
     */
    var VocReducingPlan = {
        tableId: "vocReducingPlanTable"
    };

    /**
     * 初始化表格的列
     */
    VocReducingPlan.initColumn = function () {
        return [[
            {field: 'companyName', sort: true, title: '所属企业'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'reductionEstimates', sort: true, title: '减排量估算'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    VocReducingPlan.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(VocReducingPlan.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    VocReducingPlan.openAddDlg = function () {
        func.open({
            title: '添加VOCs综合减排措施',
            content: Feng.ctxPath + '/vocReducingPlan/add',
            tableId: VocReducingPlan.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    VocReducingPlan.openEditDlg = function (data) {
        func.open({
            title: '修改VOCs综合减排措施',
            content: Feng.ctxPath + '/vocReducingPlan/edit?id=' + data.id,
            tableId: VocReducingPlan.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    VocReducingPlan.exportExcel = function () {
        var checkRows = table.checkStatus(VocReducingPlan.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    VocReducingPlan.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/vocReducingPlan/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(VocReducingPlan.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + VocReducingPlan.tableId,
        url: Feng.ctxPath + '/vocReducingPlan/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: VocReducingPlan.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        VocReducingPlan.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        VocReducingPlan.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        VocReducingPlan.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + VocReducingPlan.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            VocReducingPlan.openEditDlg(data);
        } else if (layEvent === 'delete') {
            VocReducingPlan.onDeleteItem(data);
        }else if(layEvent === 'sourceEdit'){
            VocReducingPlan.openSource(data);
        }else if(layEvent === 'processEdit'){
            VocReducingPlan.openProcess(data);
        }else if(layEvent === 'terminalEdit'){
            VocReducingPlan.openTerminal(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

         VocReducingPlan.changeDelFlag(id, checked);
    });

    /**
     * 修改状态
     *
     * @param userId 用户id
     * @param checked 是否选中（true,false），选中就是解锁用户，未选中就是锁定用户
     */
     VocReducingPlan.changeDelFlag = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/vocReducingPlan/enable", function (data) {
                if (data.code == 200) {
                    Feng.success("启用成功!");
                }else{
                    Feng.error("启用失败!" + data.message + "!");
                }
                table.reload( VocReducingPlan.tableId);
            }, function (data) {
                Feng.error("启用失败!" + data.message + "!");
                table.reload( VocReducingPlan.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/vocReducingPlan/disable", function (data) {
                if(data.code == 200) {
                    Feng.success("禁用成功!");
                }else{
                    Feng.error("禁用失败!" + data.message + "!");
                }
                table.reload( VocReducingPlan.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.message + "!");
                table.reload( VocReducingPlan.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        }
    };


     /*源头替代编辑*/
    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    VocReducingPlan.openSource = function (data) {
         var  redType  = 1 ;
        func.open({
            title: '编辑源头替代',
            content: Feng.ctxPath + '/vocReducingPlan/sourceEdit?id=' + data.id + '&redType='+1,
            tableId: VocReducingPlan.tableId
        });
    };



    /*编辑过程控制*/
    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    VocReducingPlan.openProcess  = function (data) {
        var  redType  = 2 ;
        func.open({
            title: '编辑过程控制',
            content: Feng.ctxPath + '/vocReducingPlan/sourceEdit?id=' + data.id + '&redType='+2,
            tableId: VocReducingPlan.tableId
        });
    };



    /*编辑末端治理*/
    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    VocReducingPlan.openTerminal = function (data) {
        var  redType  = 3 ;
        func.open({
            title: '编辑末端治理',
            content: Feng.ctxPath + '/vocReducingPlan/sourceEdit?id=' + data.id + '&redType='+3,
            tableId: VocReducingPlan.tableId
        });
    };
});
